/* global window */
import modelExtend from 'dva-model-extend'
import pathToRegexp from 'path-to-regexp'
import * as basicData from 'services/basicData.js'
import { list, create, update, remove } from 'services/address'
import { message } from 'antd'
import { pageModel } from './common'

export default modelExtend(pageModel, {
  namespace: 'addressData',

  state: {
    addressType: '',
    parentId: '',
    contractTypeData: [],
    countryCodeData: [],
  },

  subscriptions: {
    setup ({ dispatch, history }) {
      history.listen((pathname) => {
        const match = pathToRegexp('/address/:id/:type').exec(pathname.pathname)
        if (match) {
          dispatch({ type: 'list', payload: { id: match[1], type: match[2] } })
          dispatch({ type: 'iniBasicData' })
          dispatch({ type: 'updateState', payload: { parentId: match[1], addressType: match[2] } })
        }
      })
    },
  },

  effects: {
    * list ({ payload = {} }, { call, put }) {
      const data = yield call(list, payload)
      const listData = data
      if (data) {
        yield put({
          type: 'updateState',
          payload: {
            list: listData.data,
            pagination: {
              current: Number(payload.page) || 1,
              pageSize: Number(payload.pageSize) || 10,
              total: listData.total,
            },
          },
        })
      }
    },

    * delete ({ payload }, { call, put, select }) {
      const data = yield call(remove, { ...payload })
      const { selectedRowKeys, addressType, parentId } = yield select(_ => _.addressData)
      if (data.success) {
        message.success('delete success')
        yield put({ type: 'updateState', payload: { selectedRowKeys: selectedRowKeys.filter(_ => _ !== payload) } })
        yield put({ type: 'list', payload: { id: parentId, type: addressType } })
      } else {
        message.error('delete failed')
        throw data
      }
    },

    * create ({ payload }, { select, call, put }) {
      const { addressType, parentId } = yield select(_ => _.addressData)
      payload.type = addressType
      const data = yield call(create, payload)
      if (data.success) {
        message.success('create success')
        yield put({ type: 'hideModal' })
        yield put({ type: 'list', payload: { id: parentId, type: addressType } })
      } else {
        message.error('create failed')
        throw data
      }
    },

    * update ({ payload }, { select, call, put }) {
      const { parentId, addressType } = yield select(_ => _.addressData)
      payload.type = addressType
      const data = yield call(update, payload)
      if (data.success) {
        message.success('update success')
        yield put({ type: 'hideModal' })
        yield put({ type: 'list', payload: { id: parentId, type: addressType } })
      } else {
        message.error('update failed')
        throw data
      }
    },

    * showModal ({ payload }, { call, put }) {
      yield put({
        type: 'updateModalState',
        payload: { ...payload },
      })
    },

    * iniBasicData({ payload = {} }, { call, put }) {
      const contractType = yield call(basicData.listall, { type: 'ContactType' })
      if (contractType.success) {
        yield put({
          type: 'updateState',
          payload: {
            contractTypeData: contractType.data,
          },
        })
      }

      const countryCodeData = yield call(basicData.listall, { type: 'CountryCodes' })
      if (countryCodeData.success) {
        yield put({
          type: 'updateState',
          payload: {
            countryCodeData: countryCodeData.data,
          },
        })
      }
    },

  },

  reducers: {
  },
})
